/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-08-07 07:51:26
 * @LastEditors: swc
 * @LastEditTime: 2023-12-30 10:37:36
 */
import { ifs, ts } from '@/services';
import { N_oRefGraph, N_oSetupPanel } from '@/types';
import { N_openGraphData } from './openGraphData/types';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: N_oRefGraph.oRefGraph;

  public tableName: string;

  public dbType: string;

  private _graphEditorState: string = 'none';

  // 当前父ID
  public currParentID: string;

  // 关联字段
  public associationField: string;

  // 节点栏位
  public nodeColumns: any;

  public tableColumns: any;

  public tableColumnData: Map<string, any> = new Map();

  public nodeColumnData: Map<string, Array<ifs.I_fv>> = new Map();

  private oOpenGraphData: N_openGraphData.openGraphData = null;

  public searchEventMsg: any = null;

  private TCoordination: ifs.I_compCoordination = null;

  public set graphEditorState(p: string) {
    this._graphEditorState = p;
    this.self.nodeAttrDisabled = p === 'none';
    this.self.nodeAttrLabelDisabled = p === 'none';
  }

  public get graphEditorState(): string {
    return this._graphEditorState;
  }

  // 主组件created里初始化
  public init(self: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  public openData() {
    if (this.oOpenGraphData === null) {
      this.oOpenGraphData = this.compBox.getCompMergeName(
        this.compID,
        'oOpenGraphData'
      );
    }
    this.oOpenGraphData.setDisplay(true);
  }

  public getGraph(): any {
    return this.self.refFlowchart.GT_editor.getGraph();
  }

  public setPermission(conf: any) {
    const toolBarPms = conf?.permission.filter(
      el => el.area === 'toolbar' || el.area === 'default'
    );
    for (const ele of toolBarPms) {
      if (ele?.data?.browse && ele.data.browse === 0) {
        this.self.refFlowchart.hideToolbar();
        break;
      }
      ele?.data?.audit === 0 && this.self.refFlowchart.hideToolbarItem('audit');
      ele?.data?.unaudit === 0 &&
        this.self.refFlowchart.hideToolbarItem('unaudit');
      ele?.data?.new === 0 && this.self.refFlowchart.hideToolbarItem('new');
      ele?.data?.edit === 0 && this.self.refFlowchart.hideToolbarItem('edit');
      ele?.data?.delete === 0 &&
        this.self.refFlowchart.hideToolbarItem('delete');
    }
  }

  // 构建组件协作处理类
  public newTcompCoordination(conf: any) {
    // 组件调度
    this.TCoordination = new ts.T_compCoordination(
      conf.name,
      conf.messageConfig,
      {
        setTempToolbarStatusDisabled: (
          p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
        ) => {
          this.setTempToolbarStatusDisabled(p);
        },
        setTempToolbarStatusUnDisabled: (
          p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
        ) => {
          this.setTempToolbarStatusUnDisabled(p);
        },
        cleanSubCompData: (
          p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
        ) => {
          this.self.cleanSubCompData(p);
        },
        setMask: (p: boolean) => {
          this.self.isMask = p;
        }
      }
    );
  }

  // 处理组件协调
  public receEventCoordination(
    m: N_oSetupPanel.N_comps.I_eventCoordinationMsg
  ) {
    this.TCoordination.receEventCoordination(m)
      .then(([b, msg]) => {})
      .catch(msg => {
        console.error(msg);
        // this.self.$msg(msg);
      });
  }

  private setTempToolbarStatusDisabled(
    p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
  ) {}

  private setTempToolbarStatusUnDisabled(
    p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
  ) {}

  // 处理组件协调
  public sendEventCoordination(
    type: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
  ) {
    const r = this.TCoordination.sendEventCoordination(type, this.self.$bus)
      .then(([b, msg]) => {})
      .catch(msg => {
        console.error(msg);
        // this.self.$msg(msg);
      });
  }
}
interface I_cmd extends T_cmd {}

export { I_cmd, T_cmd };
